package com.rest.demo.concurrent;

/**
 * @author Administrator
 * @decription
 * @Date 2018-01-31 16:39
 */
public class DeadLockDemo {
    public static void main(String[] args) {
        Business business = new Business();
        new Thread(new Runnable() {
            @Override
            public void run() {
                while(true) {
                    business.functionA();
                }
            }
        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    business.functionB();
                }
            }
        }).start();
    }
}

class Business { //定义两个锁，两个方法
    //定义两个锁
    public static final Object lock_a = new Object();
    public static final Object lock_b = new Object();

    public void functionA() {
        synchronized(lock_a) {
            System.out.println("---ThreadA---lock_a---");
            synchronized(lock_b) {
                System.out.println("---ThreadA---lock_b---");
            }
        }
    }

    public void functionB() {
        synchronized(lock_b) {
            System.out.println("---ThreadB---lock_b---");
            synchronized(lock_a) {
                System.out.println("---ThreadB---lock_a---");
            }
        }
    }
}
